home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / info / vgophong.txt < prev    next >
Text File  |  1999-03-27  |  18KB  |  466 lines

  1. -----------------------------------------------------------------------------
  2.  
  3.   VGOPHONG.TXT - Phong lighting and specular highlights. Theory,
  4.          practice and explaination of the phong lighting
  5.          and shading model.
  6.  
  7. -----------------------------------------------------------------------------
  8.  
  9.   by TimJ/Vertigo
  10.  
  11.   "I am he, as you are he, as you are me, and we are all together"
  12.  
  13.   email: tim@legend.co.uk
  14.   irc:   #coders #vertcode
  15.  
  16.  
  17.   revision history:
  18.  
  19.   16/02/97      v1.0    -       Initial version
  20.  
  21.  
  22. -----------------------------------------------------------------------------
  23.  INTRODUCTION
  24. -----------------------------------------------------------------------------
  25.  
  26.   First off, I hope this doc is of use to some people, and maybe other
  27.   people will find it interesting.
  28.  
  29.   Recently, I've been thinking a lot about phong shading and lighting.
  30.   There was something that was bugging me. I couldn't quite put my finger
  31.   on it. It was something I knew to be true, but I had to explain it to
  32.   myself. It all started when I was chatting to Vector about true phong
  33.   shading. We'd both recently looked at Voltaire/OTM's doc on fast phong
  34.   shading (again).
  35.  
  36.   We were (among other things) chatting about lighting functions in our
  37.   3D engine. It was about specular highlights and the way normal (fast)
  38.   phong lighting doesn't yield specular highlights on a flat plane
  39.   (all normals pointing one way). This got me thinking. I thought, well
  40.   of course it doesn't, because the light calculated at each point will be
  41.   the same (because all normals are the same). This annoyed me because I
  42.   knew it wasn't true -- but I couldn't remember why. It's all to do with
  43.   the light vector and the view vector (and keeping them constant).
  44.  
  45.   Volatire's phong method also doesn't yield specular highlights in the
  46.   center of polygons. Oh, and his method emulates the equation given
  47.   exactly -- it's not a tradeoff. But then again, it's also just the
  48.   same as using gouraud. I'll explain later.
  49.  
  50.   Then I remembered the actual theory behind the lighting equations. It
  51.   stuck me that people tend to get mixed up in code and forget about the
  52.   theory behind it.
  53.  
  54.   What I'll do is go through the theory, how it's implemented.
  55.   Based on that I'll then address what this doc is actually about --
  56.   highlights in the center of polygons and on flat planes.
  57.  
  58.   If you think this doc is a bit slow, forgive me, but you can never please
  59.   everybody :)
  60.  
  61.   [Oh, important point.. this is generally about the phong equation. You
  62.    can do it per pixel or per vertex, either way it's the phong equation.
  63.    Just because you gouraud shade doesn't mean you can't have phong style
  64.    specular highlights.]
  65.  
  66.  
  67. -----------------------------------------------------------------------------
  68.  LIGHT RAY REFLECTION
  69. -----------------------------------------------------------------------------
  70.  
  71.   I was going to explain the theory behind light ray relfection and the
  72.   spread of the ray across a surface depending on the angle of incidence.
  73.   But but then I realized I'd have to go into they physics behind spectral
  74.   reflectivity too, so I won't :)
  75.  
  76.   If there's enough demand for it, email me and I'll put it in.
  77.  
  78.  
  79. -----------------------------------------------------------------------------
  80.  PHONG'S SPECULAR HIGHLIGHTS
  81. -----------------------------------------------------------------------------
  82.  
  83.   We need to understand how the phong lighting equation is made up. Let's
  84.   define a few useful values:
  85.  
  86.  
  87.           ^N
  88.           |
  89.         L     |     R        V
  90.          \    |    /      __/
  91.           \   |   /    __/
  92.            \  |  /  __/
  93.         \ | /__/
  94.          \|//
  95.      -------------.--------------
  96.           P
  97.           ^
  98.           point under consideration
  99.  
  100.  
  101.   It's important you know what these values actually are:
  102.  
  103.   N = surface normal
  104.   L     = unit vector between point and light
  105.   V = unit vector between point and view
  106.   R     = light reflection unit vector (mirror of L about N)
  107.  
  108.  
  109.   First, the diffuse relfection is given by the Lamertian Relfection
  110.   equation:
  111.  
  112.     diffuse = Kd * (N dot L)
  113.  
  114.   Where Kd is the diffuse relfection constant. (N dot L) is the same as
  115.   the cosine of the angle between N and L, so as the angle decrease, the
  116.   resulting diffuse value is higher.
  117.  
  118.   Phong gave spectral reflectivity as:
  119.  
  120.     diffuse + Ks * (R dot V)^n
  121.  
  122.   Which is:
  123.  
  124.     Kd * (N dot L) + Ks * (R dot V)^n
  125.  
  126.   Where Kd is the diffuse component and Ks is the specular compoenet. This
  127.   is the generally accepted phong lighting equation. Ks is generaly taken to
  128.   be a specularity constant (although Phong defined it as W(i).. see later).
  129.  
  130.   As the angle between the view (V) and the reflected light (R) decreases,
  131.   you will get more specularity.
  132.  
  133.   The clever thing about Phong's equation was that it gave a neat way to
  134.   calculate the specular intensity 'bump' around the light reflection
  135.   vector (R). The larger the exponential power (n) the smaller and more
  136.   intense the specular intensity bump. Hence specular highlights.
  137.  
  138.  
  139. -----------------------------------------------------------------------------
  140.  IMPLEMENTATION OF PHONG'S EQUATION
  141. -----------------------------------------------------------------------------
  142.  
  143.   Most people simplify this equation somewhat, for speed. We begin with :
  144.  
  145.     Kd * (N dot L) + Ks * (R dot V)^n
  146.  
  147.   The obvious thing we'd like to remove is (R dot V). Since we don't
  148.   want to calculate the light relfection vector (mirror of light incidence
  149.   around the surface normal) -- because it's expensive. Blinn introduced a
  150.   way to do this using an imaginary vector H. It's then reduced to (N dot H).
  151.   H is defined as halfway between L and V (after L and V are normalized).
  152.  
  153.   H is therefore (L + V) / 2. You will see that the angle R dot V is double
  154.   N dot H -- but this doesn't matter as you can alter the specular
  155.   exponential value (n) to compenstate. This gives us the equation :
  156.  
  157.     Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  158.  
  159.   Up until now we've ignored the ambient factor, this is because it's
  160.   damn obvious and has little consequence on the math.. we'll put it in
  161.   now
  162.  
  163.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  164.  
  165.   Which is easily implemented. You only need three vectors: the surface
  166.   normal, the light vector and the view vector. It's obviously advised
  167.   to do this equation in object-space.
  168.  
  169.  
  170.   Another way to remove R dot V, is by replacing it with N dot L :
  171.  
  172.     Ka + Kd * (N dot L) + Ks * (N dot L)^n
  173.  
  174.   This assumes you will always get the maximum specularly reflected light,
  175.   no matter where the view is. Here's why :
  176.  
  177.   If we assume V is always the same as R, then the angle between N and V is
  178.   the same as N and L --
  179.  
  180.           ^N
  181.           |                     A = angle between N and L
  182.         L     |     R (also V)      B = angle between N and V
  183.          \  A | B  /
  184.           \  /|\  /
  185.            \/ | \/
  186.         \ | /
  187.          \|/
  188.      -------------.--------------
  189.  
  190.  
  191.   Angle A and B are the same (of course, since R is the mirror vector of L).
  192.   So, N dot V becomes the same as L dot N.
  193.  
  194.   This makes life easier and faster. The results completely ignore the
  195.   position of the view; so it's like having a reflective surface that always
  196.   reflects the maximum amount of specular light towards the view.
  197.  
  198.     (normally, as the angle between the view and the reflected light
  199.      increases, you get less specularly reflected light).
  200.  
  201.   It's just a trade off.
  202.  
  203.     Ka + Kd * (N dot L) + Ks * (N dot L)^n
  204.  
  205.   or
  206.  
  207.     Ka + Kd * cos(theta) + Ks * cos(theta)^n
  208.  
  209.   where cos(theta) is N dot L. Most likely, the above equation is the
  210.   one most people use. Also, since more implementation assume V is constant
  211.   across the scene (normalized.. at infinity) then using N dot L can be
  212.   acceptable. But it does have some dire consequences.
  213.  
  214.  
  215. -----------------------------------------------------------------------------
  216.  REAL TIME PHONG SHADING
  217. -----------------------------------------------------------------------------
  218.  
  219.   This is what was causing the confusion. Voltaires text on phong shading
  220.   (OTMPHONG.TXT) used the equation
  221.  
  222.     color = specular + (cos x) * diffuse + (cos x)^n * specular
  223.  
  224.   for calculating phong lighting. This is the same as the last equation we
  225.   just disussed. He then went on to explain the specular intensity 'bump'
  226.   through the (cos x)^n term of the equation.
  227.  
  228.   (note: phong shading is done by recalculating the lighting equation at
  229.    each pixel -- this is done by interpolating the vertex normals across
  230.    the polygon and re-evaluating).
  231.  
  232.   Since there is just one angle term in the equation (N dot L), he realized
  233.   he could dispense with normals and just interpolate the angle.
  234.  
  235.   Remember :
  236.  
  237.     Ka + Kd * cos(theta) + Ks * cos(theta)^n
  238.  
  239.   You only need to interpolate theta, then you can do a lookup table for
  240.   the correct colour created like so:
  241.  
  242.   for( theta=0 ; theta < 90 ; theta++ )
  243.   {
  244.     table[i] = Ka + Kd * cos(theta) + Ks * pow( cos(theta) , specExp ) ;
  245.   }
  246.  
  247.   The problem with all this is that the original equation was inaccurate,
  248.   so the results will be inaccurate. However, Voltaire does point this
  249.   out, and state that highlights can't be inside polygons.
  250.  
  251.   But, as Zog pointed out to me, you can get exactly the same effect with
  252.   gouraud, by setting up the palette in a similar way. This method is
  253.   basically the same as gouraud, you're just interpolating an angle instead
  254.   of an intensity.. as Zog put it, "it's fucking gouraud revisited" :)
  255.  
  256.   I just thought I'd clear this up, as people tend not to believe it's
  257.   real, and think it's some kind of trick (if you use the same equation
  258.   in a true shader, you'll get the same results).
  259.   It's also important for the next section (the equation at least).
  260.  
  261. -----------------------------------------------------------------------------
  262.  PHONG'S SPECULAR HIGHLIGHTS (REVISITED)
  263. -----------------------------------------------------------------------------
  264.  
  265.   As pointed out, phong shading requires the interpolating of vertex
  266.   normals across the polygon, and recalculation of the equation.
  267.  
  268.   Right, here's where more confustion comes in. To simplify things,
  269.   people tend to treat V as a constant over the entire scene.
  270.  
  271.                  N
  272.              L        |    / V (view)
  273.            (light) \      |   /
  274.              \    |  /
  275.                \  | /
  276.                  \|/
  277.          -------------.--------------
  278.                   P
  279.  
  280.  
  281.  
  282.   V is not constant though.. it is dependant on the point under
  283.   consideration (P). So making V constant, is like sticking the view at
  284.   infinity (this is done by normalizing the view vector).
  285.   This means that the falloff of the specular light at sharp angles
  286.   between the surface and view is not taken into account (it's linear).
  287.   So the highlight will be too big and intense at sharp angles (the
  288.   falloff will be linear in respect to the view position). Also, the
  289.   highlight won't move correctly with the view.
  290.  
  291.   You can probably see that the same thing can be done for L..
  292.   ie. directional lighting. Putting L at infinity affects specular fall
  293.   off with respect to the light and the view. But I'm sure everyone knows
  294.   the implications of directional lighting (it's just like having a light
  295.   really really far away).
  296.  
  297.  
  298.   As explained earlier, dispensing with V altogether can give you a nice
  299.   speed up using N dot L instead of R dot V.
  300.  
  301.   Let's look at the consequences of dispensing with V. This is like
  302.   assuming you have a perfect reflector.. like a mirror. The surface will
  303.   always reflect the maximum amount of specular light towards the view --
  304.   the highlight will seem to 'stick' to the light relfection vector and not
  305.   change shape or size, no matter where you put the view.
  306.  
  307.   But, given this, you just have to interpolate N for the polygon
  308.  
  309.     (remember, we need (N dot L) and (N dot H) where is H = L+V/2)
  310.  
  311.  
  312. -----------------------------------------------------------------------------
  313.  SPECULAR REFLECTION
  314. -----------------------------------------------------------------------------
  315.  
  316.   Now we get to the crux of the problem. All that time ago back at the
  317.   start, I mentioned a plane with all the normals pointing one way.
  318.  
  319.   The thing to remember is this, the lighting is *not* just dependant on
  320.   the surface normal. It's a function of the light vector and the view
  321.   vector. The important value is V, as V affects how specular light is
  322.   reflected.
  323.  
  324.   If V is properly calculated, for a flat surface, the angle between the
  325.   view and the normal (which is constant) will alter.
  326.  
  327.                   N
  328.     V                    ^
  329.     \ \                  |
  330.      \   \               |
  331.       \     \            |
  332.        \       \         |
  333.         \         \      |
  334.          \           \   |
  335.        -------.-----------.---
  336.           p1          p2
  337.  
  338.  
  339.   The angle at p1 is obviously sharper than the angle at p2. Even though
  340.   N is the same at both points. It all becomes obvious now. Unless you
  341.   calculate V correctly, the reflected specular light over a flat surface
  342.   will be the even at any point on it.
  343.  
  344.   At this point you might be thinking : "what if V is put at infinity and
  345.   L is calculated properly? -- won't that do the same job?"
  346.  
  347.   In a word, no, because V is the important vector. We have to remember the
  348.   original equation where the specular light was a function of R and V.
  349.   N would be constant, V would be constant, so specular light would just be
  350.   a function of V -- ie, not very accurate at all.
  351.  
  352.   So, we're left with the following equation:
  353.  
  354.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  355.  
  356.   So, basically, in a nutshell, you've got to recalculate V for the new
  357.   point under consideration. If you do that, you'll get specular highlights
  358.   on flat planes.
  359.  
  360.   If you have directional lights (L at infinity) then the highlight will be
  361.   incorrectly positioned and spread -- since the angle between any given
  362.   point and the light will be more or less the same (to an infintecial
  363.   degree). On a flat surface you'd be depending on the angle the view makes
  364.   with the surface. Although, this isn't too much to worry about --
  365.   I think directional lighting is a choice, not a compromise.
  366.  
  367.     [ Oh yeah, in the first section I said Phong defined Ks as W(i). Well,
  368.       this meant that Ks was a function of the angle of incidence between
  369.       the light and the surface. So specularly reflected light was dependant
  370.       on the incoming angle as well as the outgoing angle. Phong never
  371.       actually defined W(i) though -- so it's usually ignored. It does give
  372.       you another parameter for your surface to play with though. ]
  373.  
  374. -----------------------------------------------------------------------------
  375.  REAL TIME PHONG SHADING (REVISIED)
  376. -----------------------------------------------------------------------------
  377.  
  378.   Ok, so now we know that V and L are important factors of the equation.
  379.   Voltaire's phong shading method is completely correct for the equation
  380.   he used (but as mentioned, it's basically the same as gouraud).
  381.  
  382.   What he did was place L at infinity and make the surface a perfect
  383.   reflector (that always reflected the maximum specular light) towards
  384.   the view (where ever it was). I explained all this earlier anyway :)
  385.  
  386.   That meant there was only one angle to interpolate, but it also meant
  387.   that on flat surfaces it was impossible to get correct highlights.
  388.   (polygons are flat :)
  389.  
  390.   However, Voltaire's method is more accurate than normal gouraud for the
  391.   same lighting equation (due to the non-linear lookup table).
  392.   However, you can do the same with gouraud.. it just another way of
  393.   implenenting it (and quite redundant).
  394.   His method won't extend to other equations though.
  395.  
  396.   What's annoying is when people just say his method doesn't work and is
  397.   a load of crap, without explaining why.
  398.  
  399.  
  400. -----------------------------------------------------------------------------
  401.  CLOSING WORDS
  402.  
  403.  "picture yourself in boat on river with tangerine trees and marmalade skies"
  404.  
  405. -----------------------------------------------------------------------------
  406.  
  407.   Well, I guess I've managed to confuse almost eveyone. What I've tried
  408.   to do is explain the factors of the phong lighting equation -- what parts
  409.   do what, and why it works.
  410.  
  411.   I've probably made loads of typo's and got different bit's mixed up --
  412.   but hell, I don't care :)  With any luck, I might not get flamed.
  413.   If I have mixed something up, forgive me -- it get's hard to track what
  414.   you have/have not said etc.. and it's not easy without some good
  415.   diagrams :)
  416.  
  417.   There are a couple of sections I've left out.. I started the one on
  418.   light ray reflection, but left it out. I also has a section on optimizing
  419.   the equation :
  420.  
  421.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  422.  
  423.   But I didn't get it finished (it's based on the routines I use in my
  424.   3D engine). There are no square roots, tables, divides or pow()'s used.
  425.   Yet it still produces the same results (to a reasonable degree of
  426.   accuracy -- any errors are covered up by (a). the number of intensities we
  427.   can actually display and (b). the precision of the fpu).
  428.   If there's enough demand then I'll put it in.
  429.  
  430.   At the end of all this, an interesting thing to note is that all these
  431.   equations have no physical basic what so ever -- they're just equations
  432.   that fit real work observations. Light is actually better represented as
  433.   radiation -- but let's not get into that now :)
  434.  
  435.   Greets:
  436.     Oh god, I don't know.. um.. people I know.. hmmm
  437.     (no particular order)
  438.  
  439.     Vector
  440.     Vastator
  441.     Phred
  442.     Gooroo
  443.     Midnight
  444.     aM
  445.     Eckart
  446.     codex
  447.     PGM (where ever he may be)
  448.     BigCheese
  449.     Pel
  450.     Wog (Zog, whatever)
  451.     Crom
  452.     God
  453.     All at Abstract Entertainment
  454.  
  455.     [plus anyone I've missed]
  456.  
  457.     Flames/comments go to: tim@legend.co.uk
  458.  
  459.  
  460. -----------------------------------------------------------------------------
  461.  
  462.   "Living is easy with eyes closed. Misunderstanding all you see.
  463.    It's getting hard to be someone, but it all works out. It doesn't matter
  464.    much to me."
  465.  
  466.